package com.dushibao.service.impl;

import com.dushibao.dao.GoodsDao;
import com.dushibao.entity.Goods;
import com.dushibao.service.GoodsService;
import com.dushibao.utils.Page;
import com.dushibao.utils.SecurityHolder;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 商品 实现类
 *
 * @author zqd
 *
 * @date 2022-06-28 15:49:19
 */
public class GoodsServiceImpl implements GoodsService {
       GoodsDao goodsDao = new GoodsDao();

       @Override
       public Integer save(Goods goods) {
            return goodsDao.save(goods);
       }

       @Override
       public Page page() {
           HttpServletRequest req = SecurityHolder.request.get();
           String current = req.getParameter("current");
           String pageSize = req.getParameter("pageSize");

           String startTime = req.getParameter("startTime");
           String endTime = req.getParameter("endTime");

           Map<String,Object> params = new HashMap<>();
           params.put("startTime",startTime);
           params.put("endTime",endTime);

           req.setAttribute("startTime",startTime);
           req.setAttribute("endTime",endTime);

           Page page = new Page();
           page.setCurrent(Integer.parseInt(current==null?"1":current));
           page.setPageSize(Integer.parseInt(pageSize==null?"10":pageSize));

           return goodsDao.page(page,params);
       }

       @Override
       public Goods getById(long id) {
            return goodsDao.getById(id);
       }

       @Override
       public Integer saveOrUpdate(Goods goods) {
            if(goods.getId()!=null){
                 return goodsDao.update(goods);
            }
            return goodsDao.save(goods);
       }

       @Override
       public Integer deleteById(List<Long> ids) {
            return goodsDao.deleteById(ids);
       }
}
